package com.func.domain.rebate.service;

import com.func.domain.award.model.valobj.TaskStateVO;
import com.func.domain.rebate.event.SendRebateMessageEvent;
import com.func.domain.rebate.model.aggerate.BehaviorRebateAggregate;
import com.func.domain.rebate.model.entity.BehaviorEntity;
import com.func.domain.rebate.model.entity.BehaviorRebateOrderEntity;
import com.func.domain.rebate.model.entity.TaskEntity;
import com.func.domain.rebate.model.valobj.DailyBehaviorRebateTypeVO;
import com.func.domain.rebate.repository.IBehaviorRebateRepository;
import com.func.types.common.Constants;
import com.func.types.event.BaseEvent;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * BehaviorRebateService
 * </p>
 *
 * @author Simon
 * @version 2025.03.17.1.0.0
 * @description 行为返利服务实现
 * @since 2025-03-17
 */

@Service
public class BehaviorRebateService implements IBehaviorRebateService{
    @Resource
    private IBehaviorRebateRepository behaviorRebateRepository;
    @Resource
    private SendRebateMessageEvent sendRebateMessageEvent;

    @Override
    public List<String> createUserBehaviorRebateOrder(BehaviorEntity behaviorEntity) {
        // 1. 查询返利配置
        List<DailyBehaviorRebateTypeVO> dailyBehaviorRebateTypeVOS =  behaviorRebateRepository.queryDailyBehaviorRebateConfig(behaviorEntity.getBehaviorTypeVO());
        if (null == dailyBehaviorRebateTypeVOS || dailyBehaviorRebateTypeVOS.isEmpty()) return new ArrayList<>();

        // 2. 构建聚合对象
        List<String> orderIds = new ArrayList<>();
        List<BehaviorRebateAggregate> behaviorRebateAggregates = new ArrayList<>();
        for (DailyBehaviorRebateTypeVO dailyBehaviorRebateTypeVO : dailyBehaviorRebateTypeVOS) {
            // 拼装业务ID: 用户ID_返利类型_外部透彻业务ID
            String bizId = behaviorEntity.getUserId() + Constants.UNDERLINE + dailyBehaviorRebateTypeVO.getRebateType() + Constants.UNDERLINE + behaviorEntity.getOutBusinessNo();
            BehaviorRebateOrderEntity behaviorRebateOrderEntity = BehaviorRebateOrderEntity.builder()
                        .userId(behaviorEntity.getUserId())
                        .orderId(RandomStringUtils.randomNumeric(12))
                        .behaviorType(dailyBehaviorRebateTypeVO.getBehaviorType())
                        .rebateDesc(dailyBehaviorRebateTypeVO.getRebateDesc())
                        .rebateType(dailyBehaviorRebateTypeVO.getRebateType())
                        .rebateConfig(dailyBehaviorRebateTypeVO.getConfig())
                        .outBusinessNo(behaviorEntity.getOutBusinessNo())
                        .bizId(bizId)
                        .build();
            orderIds.add(behaviorRebateOrderEntity.getOrderId());

            // MQ 消息对象
           SendRebateMessageEvent.RebateMessage rebateMessage = SendRebateMessageEvent.RebateMessage.builder()
                                              .userId(behaviorRebateOrderEntity.getUserId())
                                              .rebateDesc(behaviorRebateOrderEntity.getRebateDesc())
                                              .rebateType(behaviorRebateOrderEntity.getRebateType())
                                              .rebateConfig(behaviorRebateOrderEntity.getRebateConfig())
                                              .bizId(behaviorRebateOrderEntity.getBizId())
                                              .build();

            // 构建事件消息
            BaseEvent.EventMessage<SendRebateMessageEvent.RebateMessage> rebateMessageBaseEvent = sendRebateMessageEvent.buildEventMessage(rebateMessage);

            // 组装任务对象
            TaskEntity taskEntity = new TaskEntity();
            taskEntity.setMessage(rebateMessageBaseEvent);
            taskEntity.setUserId(behaviorEntity.getUserId());
            taskEntity.setTopic(sendRebateMessageEvent.topic());
            taskEntity.setMessageId(rebateMessageBaseEvent.getId());
            taskEntity.setState(TaskStateVO.create);

            BehaviorRebateAggregate behaviorRebateAggregate = BehaviorRebateAggregate.builder()
                    .behaviorRebateOrderEntity(behaviorRebateOrderEntity)
                    .taskEntity(taskEntity)
                    .build();

            behaviorRebateAggregates.add(behaviorRebateAggregate);
        }

        // 3. 存储聚合对象数据
        behaviorRebateRepository.saveUserRebateRecord(behaviorEntity.getUserId(), behaviorRebateAggregates);

        // 4. 返回订单ID集合
        return orderIds;
    }

    @Override
    public List<BehaviorRebateOrderEntity> queryOrderByOutBusinessNo(String userId, String outBusinessNo) {
        return behaviorRebateRepository.queryOrderByOutBusinessNo(userId, outBusinessNo);
    }
}
